<!DOCTYPE html>
<html>
<head>
    {include file="public/header" /}
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">题目名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="search" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="edit"> 保存</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>
{include file="public/footer" /}
<script type="text/html" id="typeTpl">
    {{#  if(d.type == 1){ }}
    <span style="">单选</span>
    {{# }else if(d.type == 2){ }}
    <span style="">多选</span>
    {{#  } }}
</script>
<script>
    var id_con=[{$id_con}];
    var all_id_con=[];
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate=layui.laydate;
        table.render({
            elem: '#currentTableId',
            url: "/admin/Active/set_question?action=ajax",
            method: 'post',
            where:{
                id_con:id_con.join(',')
            },
            parseData: function (res) { //res 即为原始返回的数据
               if(res.data.id_con!=''&&res.data.id_con!=undefined){
                   all_id_con=res.data.id_con;
               }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data, //解析数据列表
                };
            },
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {field:'id',type:'checkbox'},
                {field: 'id', title: '序号'},
                {field: 'title', title: '题目名称'},
                {field: 'type', title: '题目类型',templet: '#typeTpl'},
            ]],
            done: function(res, page, count){
                // 后台设置每条数据LAY_CHECKED关键字  选中填写任意值 不选中不写值
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: false,
            skin: 'line',


        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                 where: {
                    searchParams: data.field,
                    id_con:id_con.join(',')
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {  // 监听添加操作
                var checkStatus=table.checkStatus(obj.config.id),
                    data=checkStatus.data;
                    if(data==""||data==undefined){
                        layer.msg("请选择题目");
                        return false;
                    }
                    parent.setQuestionModel(data);
                    var iframeIndex = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(iframeIndex);
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId'),
                    data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            } else if (obj.event === 'LAYTABLE_TIPS') {
                alert('自定义的,不用的话可以将该方法删除')
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            if(obj.type=='all'){
                id_con=all_id_con;
            }else{
                if(obj.checked){
                    // 选中
                    id_con.push(obj.data.id);
                }else{
                    let key=id_con.indexOf(obj.data.id);
                    id_con.splice(key, 1);
                }
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/admin/Active/question_write?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('确定要删除吗？', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "/admin/Active/question_delete",
                        type: 'post',
                        async: true,
                        data: {
                            id:data.id
                        },
                        dataType: 'json',
                        success: function (res) {
                            if(res.status){
                                parent.layer.msg("操作成功!", {icon:1,time: 1000}, function () {
                                    //重新加载父页面
                                    obj.del();

                                });
                                return;
                            }else{
                                layer.msg(res.msg);
                            }
                        }
                    });
                });
            }
        });

    });
</script>

</body>
</html>